from collections import namedtuple
from flask import Flask
from flask.json import jsonify
from flask_cors import *
from shost import *


class Point:
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2


app = Flask(__name__)
CORS(app, supports_credentials=True)


@app.route('/pts')
def hello_world():
    curr_day = datetime.now().strftime("%Y-%m-%d")
    if curr_day not in days_dict:
        return jsonify([])
    res = []
    last_p = None
    for d in days_dict[curr_day]:
        p = d_list[d]
        if last_p:
            res.append({'x1': last_p[0], 'x2': p[0],
                        'y1': last_p[1], 'y2': p[1]})
        last_p = p
    return jsonify(res)
    # return jsonify([
    #     {'x1': 14, 'x2': 30, 'y1': 41, 'y2': 42},
    #     {'x1': 30, 'x2': 40, 'y1': 42, 'y2': 32},
    #     {'x1': 40, 'x2': 46, 'y1': 32, 'y2': 32},
    #     {'x1': 46, 'x2': 46, 'y1': 32, 'y2': 29},
    #     {'x1': 46, 'x2': 55, 'y1': 29, 'y2': 30},
    #     {'x1': 30, 'x2': 55, 'y1': 42, 'y2': 30},
    # ])


@app.route('/clear')
def clear_pts():
    curr_day = datetime.now().strftime("%Y-%m-%d")
    days_dict[curr_day] = []
    return 'success'


if __name__ == '__main__':
    t1 = threading.Thread(target=recs, args=(), name='rec')
    t1.start()
    app.run(host='0.0.0.0', port=54432)
